﻿using System.Runtime.InteropServices;
using System.Text;

namespace NativeInterop
{
    public class Kernel32
    {
        //检索系统目录的路径
        [UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Unicode)]
        public delegate uint GetSystemDirectoryW([Out] StringBuilder lpBuffer, uint uSize);

        //检索有关当前系统的信息
        [StructLayout(LayoutKind.Sequential)]
        public struct SYSTEM_INFO
        {
            public ushort wProcessorArchitecture;
            public ushort wReserved;
            public uint dwPageSize;
            public IntPtr lpMinimumApplicationAddress;
            public IntPtr lpMaximumApplicationAddress;
            public IntPtr dwActiveProcessorMask;
            public uint dwNumberOfProcessors;
            public uint dwProcessorType;
            public uint dwAllocationGranularity;
            public ushort wProcessorLevel;
            public ushort wProcessorRevision;
        }
        [UnmanagedFunctionPointer(CallingConvention.StdCall)]
        public delegate void GetSystemInfo(out SYSTEM_INFO lpSystemInfo);

        //检索有关系统当前物理内存和虚拟内存使用情况的信息。
        [StructLayout(LayoutKind.Sequential)]
        public struct MEMORYSTATUSEX
        {
            public uint dwLength;
            public uint dwMemoryLoad;
            public ulong ullTotalPhys;
            public ulong ullAvailPhys;
            public ulong ullTotalPageFile;
            public ulong ullAvailPageFile;
            public ulong ullTotalVirtual;
            public ulong ullAvailVirtual;
            public ulong ullAvailExtendedVirtual;
        }
        [UnmanagedFunctionPointer(CallingConvention.StdCall)]
        public delegate void GlobalMemoryStatusEx(out MEMORYSTATUSEX lpBuffer);

        //创建文件夹
        [StructLayout(LayoutKind.Sequential)]
        public struct SECURITY_ATTRIBUTES
        {
            public uint nLength;
            public IntPtr lpSecurityDescriptor;
            [MarshalAs(UnmanagedType.Bool)]
            public bool bInheritHandle;
        }
        [UnmanagedFunctionPointer(CallingConvention.StdCall)]
        public delegate bool CreateDirectoryW(StringBuilder lpPathName, IntPtr lpSecurityAttributes);
    }

    public class Shell32
    {
        //获得系统文件路径
        [UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Unicode)]
        public delegate int SHGetFolderPathW(
        IntPtr hwnd,
        int csidl,
        IntPtr hToken,
        uint dwFlags,
        [Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszPath);
    }
        public class NativeDelegates
    {
       
        // 可扩展其他系统委托
        // 定义委托类型匹配GetSystemInfo函数签名
        


        

        [Flags]
        public enum AllocationType : uint
        {
            COMMIT = 0x1000,
            RESERVE = 0x2000,
            RESET = 0x80000,
            // 其他标志...
        }

        [Flags]
        public enum MemoryProtection : uint
        {
            EXECUTE = 0x10,
            EXECUTE_READ = 0x20,
            // 其他保护标志...
        }
    }
}
